package com.zcx.qshop.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonAddressAsyGet;
import com.zcx.qshop.conn.JsonAddressaddAsyGet;
import com.zcx.qshop.conn.JsonAddressupdateAsyGet;
import com.zcx.qshop.conn.JsonAreaAsyGet;
import com.zcx.qshop.util.UtilRegular;
import com.zcx.qshop.view.SelectView;
import com.zcx.qshop.view.TitleView;

/**
 * Created by Administrator on 2015/8/19 0019.
 */
public class CreateAddressActivity extends QSActivity implements View.OnClickListener{

    public static OnAddressChangeListener OnAddressChangeListener;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.create_address_name) private EditText name;

    @BoundView(R.id.create_address_phone) private EditText phone;

    @BoundView(R.id.create_address_location) private EditText location;

    @BoundView(R.id.create_address_city) private View city;

    @BoundView(R.id.create_address_city_text) private TextView city_text;

    @BoundView(R.id.create_address_default) private SelectView is_default;

    @BoundView(R.id.create_address_default_layout) private View layout;

    @BoundView(R.id.create_address_save) private TextView save;

    private JsonAddressAsyGet.Info.Address address;

    private JsonAreaAsyGet.Area currentArea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create_address);

        address = (JsonAddressAsyGet.Info.Address) getIntent().getSerializableExtra("Address");

        if(address != null){

            name.setText(address.name);

            phone.setText(address.mobile);

            city_text.setText(address.city);

            location.setText(address.address);

            currentArea = new JsonAreaAsyGet.Area();

            currentArea.code = address.citycode;

            layout.setVisibility(View.GONE);

            titleView.setTitleName("修改收货地址");

            save.setText("修改地址");

        }else{

            titleView.setTitleName("新增收货地址");

        }

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        OnAddressChangeListener = new OnAddressChangeListener() {

            @Override
            public void onAreaChange(JsonAreaAsyGet.Area area) {

                city_text.setText((currentArea = area).name);

            }

        };

        city.setOnClickListener(this);

        save.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.create_address_city:

                startVerifyActivity(CityActivity.class);

                break;

            case R.id.create_address_save:

                if(name.getText().toString().equals("")){

                    UtilToast.show(this, "收货人不能为空");

                }else if(phone.getText().toString().equals("")){

                    UtilToast.show(this, "手机号不能为空");

                }else if(!UtilRegular.mobile(phone.getText().toString())){

                    UtilToast.show(this, "手机格式不正确");

                }else if(currentArea == null){

                    UtilToast.show(this, "请选择城市");

                }else if(location.getText().toString().trim().equals("")){

                    UtilToast.show(this, "详细地址不能为空");

                }else{

                    if(address == null){

                        new JsonAddressaddAsyGet(QSApplication.QSPreferences.readUid(), name.getText().toString(), phone.getText().toString(), currentArea.code, location.getText().toString(), is_default.isCheck()?"1":"0", new AsyCallBack(){

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                if(ConfirmOrderActivity.onOrderChangeListener!=null){

                                    JsonAddressAsyGet.Info.Address address = new JsonAddressAsyGet.Info.Address();

                                    address.name = name.getText().toString();

                                    address.mobile = phone.getText().toString();

                                    address.city = currentArea.name;

                                    address.address = location.getText().toString();

                                    ConfirmOrderActivity.onOrderChangeListener.onAddressChange(address);

                                    QSApplication.INSTANCE.finishActivity(AddressManageActivity.class);

                                }

                                if (AddressManageActivity.onAddressChangeListener!=null){

                                    AddressManageActivity.onAddressChangeListener.onAddressChange();

                                }

                                finish();

                            }

                        }).execute(this);

                    }else{

                        new JsonAddressupdateAsyGet(QSApplication.QSPreferences.readUid(), address.aid, name.getText().toString(), phone.getText().toString(), currentArea.code, location.getText().toString(), new AsyCallBack(){

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                if (AddressManageActivity.onAddressChangeListener!=null){

                                    AddressManageActivity.onAddressChangeListener.onAddressChange();

                                }

                                UtilToast.show(CreateAddressActivity.this, "地址修改成功");

                                finish();

                            }

                        }).execute(this);

                    }

                }

                break;

        }

    }

    @Override
    protected void onDestroy() {

        OnAddressChangeListener = null;

        super.onDestroy();

    }

    public interface OnAddressChangeListener{

        void onAreaChange(JsonAreaAsyGet.Area area);

    }

}
